<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="90px">
      <el-form-item label="积分来源类" prop="cName">
        <el-select
        v-model="queryParams.accumulateType"
        filterable
        default-first-option
        remote
        placeholder=""
        class="header-search-select"
        >
        <!-- 0: 充值 1:订单消费 2:分享码 3:提现 4:人工充值 -->
        <el-option value="0" label="充值" />
        <el-option value="1" label="订单消费" />
        <el-option value="2" label="分享码" />
        <el-option value="3" label="提现" />
        <el-option value="4" label="人工充值" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
       <el-table-column label="客户名称" align="center" prop="userName" />
       <el-table-column label="积分来源" align="center">
          <template slot-scope="scope">
              <span v-html="getType(scope.row.accumulateType)"></span>
          </template>
      </el-table-column>
      <el-table-column label="来源单号" align="center" prop="sourceNo" />
      <el-table-column label="积分值" align="center" prop="accumulatePoint" />
      <!-- [0: 充值 1:订单消费 2:分享码 3:提现 4:人工充值] -->
     
      <el-table-column label="分享人名称" align="center" prop="shareToUserName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="显示顺序" align="center" prop="orderNum" >
          <template slot-scope="scope">
              {{scope.row.provinceName}}
              {{scope.row.cityName}}
          </template>
      </el-table-column> -->
      <!-- <el-table-column label="负责人" align="center" prop="leader" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:company:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:company:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getListFn"
    />

    <!-- 添加或修改公司信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="居间方名称" prop="cName">
          <el-input v-model="form.cName" placeholder="请输入居间方名称" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="所属省份" prop="provinceCode">
            <el-select v-model="form.provinceObj" @change="getAreaListFn(2,form.provinceObj.id,'')" placeholder="请选择所属省份" value-key="id" style="width: 100%">
                <el-option
                v-for="item in provinceList"
                :key="item.id"
                :label="item.area_name"
                :value="item">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="所属城市" prop="cityCode">
            <el-select v-model="form.cityObj" placeholder="请选择所属城市" value-key="id" style="width: 100%">
                <el-option
                v-for="item in cityList"
                :key="item.id"
                :label="item.area_name"
                :value="item">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="居间方状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList } from "@/api/system/flowwater";

export default {
  name: "Flowwater",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 公司信息表格数据
      infoList: [],
      // 日期范围
      dateRange: [],
      search: '',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accumulateType: undefined,
        orderNum: undefined,
        areaCode: undefined,
        leader: undefined,
        phone: undefined,
        email: undefined,
        status: undefined,
      },
      // 表单参数
      form: {
        provinceObj: undefined,
        cityObj: undefined,
      },
        // 表单校验
        rules: {},
        provinceList: [],
        cityList: [],
        isShowRecharge: false,
        accumulatePoint: null
    };
  },
  created() {
    this.getListFn();
  },
  methods: {
    //  积分来源类型
    getType(type){
        // 0:充值 1:订单消费 2:分享码 3:提现 4:人工充值
        switch(type){
            case 0:
                return '充值';
            break;
            case 1:
                return '订单消费';
            break;
            case 2:
                return '分享码';
            break;
            case 3:
                return '提现';
            break;
            case 4:
                return '人工充值';
            break;
        }
    },
    /** 查询公司信息列表 */
    getListFn() {
        this.loading = true;
        if(this.dateRange === null){
            this.dateRange = ['',''];
        }
        this.queryParams.begTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
        getList(this.queryParams).then(response => {
                this.infoList = response.rows;
                this.total = response.total;
                this.loading = false;
        });
    },
    getAreaListFn(type,id,isEdit){
        if(isEdit !== 'edit'){
            this.form.cityObj = undefined;
        }
        exportCityList({leveType: type,parentId: id}).then(res => {
            if(res.code === 'success'){
                for(let i = 0; i< res.data.length; i++){
                    delete res.data[i].parent_id;
                }
                if(type === 1){
                    this.provinceList = res.data;
                }else if(type === 2){
                    this.cityList = res.data;
                }
            }
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cId: undefined,
        cName: undefined,
        orderNum: undefined,
        areaCode: undefined,
        leader: undefined,
        phone: undefined,
        email: undefined,
        status: "0",
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        provinceObj: undefined,
        cityObj: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getListFn();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司信息";
      this.getAreaListFn(1,'','')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cId = row.cId || this.ids;
      this.getAreaListFn(1,'','');
      this.getAreaListFn(2,row.provinceCode,'');
      getInfo(cId).then(response => {
        this.form = JSON.parse(JSON.stringify(response.data));
        this.form.provinceObj = {
            id: this.form.provinceCode,
            area_name: this.form.provinceName,
        }
        let citys = {
            id: this.form.cityCode,
            area_name: this.form.cityName,
        };
        this.$set(this.form, 'cityObj', citys)
        this.open = true;
        this.title = "修改公司信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            this.form.provinceCode = this.form.provinceObj.id;
            this.form.provinceName = this.form.provinceObj.area_name;
            this.form.cityCode = this.form.cityObj.id;
            this.form.cityName = this.form.cityObj.area_name;
          if (this.form.cId != undefined) {
            updateInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getListFn();
              }
            });
          } else {
            addInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getListFn();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cIds = row.cId || this.ids;
      this.$confirm('是否确认删除公司信息编号为"' + cIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(cIds);
        }).then(() => {
          this.getListFn();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
  }
};
</script>